home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / OutOfPhase1.01Source / OutOfPhase Folder / TrashTracker.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-01  |  3.5 KB  |  113 lines  |  [TEXT/KAHL]

  1. /* TrashTracker.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "TrashTracker.h"
  31. #include "Memory.h"
  32.  
  33.  
  34. typedef struct ConsCellRec
  35.     {
  36.         void*                                    TheBlock;
  37.         struct ConsCellRec*        Next;
  38.     } ConsCellRec;
  39.  
  40.  
  41. struct TrashTrackRec
  42.     {
  43.         ConsCellRec*                    ItemList;
  44.     };
  45.  
  46.  
  47. /* create a trash tracking record */
  48. TrashTrackRec*        NewTrashTracker(void)
  49.     {
  50.         TrashTrackRec*    Trash;
  51.  
  52.         Trash = (TrashTrackRec*)AllocPtrCanFail(sizeof(TrashTrackRec),"TrashTrackRec");
  53.         if (Trash == NIL)
  54.             {
  55.              FailurePoint1:
  56.                 return NIL;
  57.             }
  58.         Trash->ItemList = NIL;
  59.         return Trash;
  60.     }
  61.  
  62.  
  63. /* dispose a trash tracking record & delete all blocks it contains */
  64. void                            DisposeTrashTracker(TrashTrackRec* Trash)
  65.     {
  66.         ConsCellRec*        Scan;
  67.  
  68.         CheckPtrExistence(Trash);
  69.         Scan = Trash->ItemList;
  70.         while (Scan != NIL)
  71.             {
  72.                 ConsCellRec*        Temp;
  73.  
  74.                 Temp = Scan;
  75.                 Scan = Scan->Next;
  76.                 ReleasePtr((char*)Temp->TheBlock);
  77.                 ReleasePtr((char*)Temp);
  78.             }
  79.         ReleasePtr((char*)Trash);
  80.     }
  81.  
  82.  
  83. /* allocate a new block & store a reference in the trash record */
  84. /* blocks allocated from this routine are normal heap blocks which may be */
  85. /* used anywhere a block from AllocPtrCanFail() may be used. */
  86. char*                            AllocTrackedBlock(long NumBytes, TrashTrackRec* Trash)
  87.     {
  88.         char*                        TheBlock;
  89.         ConsCellRec*        TrackRecord;
  90.  
  91.         CheckPtrExistence(Trash);
  92.         TheBlock = AllocPtrCanFail(NumBytes,"AllocTrackedBlock: block");
  93.         if (TheBlock == NIL)
  94.             {
  95.              FailurePoint1:
  96.                 return NIL;
  97.             }
  98.  
  99.         TrackRecord = (ConsCellRec*)AllocPtrCanFail(sizeof(ConsCellRec),
  100.             "AllocTrackedBlock: ConsCellRec");
  101.         if (TrackRecord == NIL)
  102.             {
  103.              FailurePoint2:
  104.                 ReleasePtr((char*)TheBlock);
  105.                 goto FailurePoint1;
  106.             }
  107.  
  108.         TrackRecord->TheBlock = TheBlock;
  109.         TrackRecord->Next = Trash->ItemList;
  110.         Trash->ItemList = TrackRecord;
  111.         return TheBlock;
  112.     }
  113.